using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Lyh.Train.Common.Http;
using Lyh.Train.Data;
using System.Drawing;

namespace Lyh.Train.Service
{
    public class TicketHelper
    {
        public static DataTable CreateTicketDt()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("TrainCode");
            dataTable.Columns.Add("TrainCodeKey");
            dataTable.Columns.Add("StartStation");
            dataTable.Columns.Add("AimStation");
            dataTable.Columns.Add("TakeTime");
            dataTable.Columns.Add("ShangWu");
            dataTable.Columns.Add("TeDeng");
            dataTable.Columns.Add("YiDeng");
            dataTable.Columns.Add("ErDeng");
            dataTable.Columns.Add("GaoJiRuanWo");
            dataTable.Columns.Add("RuanWo");
            dataTable.Columns.Add("YingWo");
            dataTable.Columns.Add("RuanZuo");
            dataTable.Columns.Add("YingZuo");
            dataTable.Columns.Add("WuZuo");
            dataTable.Columns.Add("QiTa");
            dataTable.Columns.Add("OrderKey");
            dataTable.Columns.Add("CanOrder");
            dataTable.Columns.Add("Order", typeof (Image));
            dataTable.Columns.Add("First");
            dataTable.Columns.Add("Last");
            dataTable.AcceptChanges();
            return dataTable;
        }

        public static DataTable GetLeftTicket(ref QueryEntity qe)
        {
            DataTable dataTable = TicketHelper.CreateTicketDt();
            if (qe.optionDates.Contains(qe.train_date))
            {
                qe.optionDates.Remove(qe.train_date);
            }
            qe.optionDates.Insert(0, qe.train_date);
            TicketService ticketService = new TicketService(Context.Cookies);
            for (int i = 0; i < qe.optionDates.Count; i++)
            {
                string train_date = qe.optionDates[i];
                qe.train_date = train_date;
                QueryTicketResponseInfo queryTicketResponseInfo = ticketService.QueryLeftTicketDTO(qe.train_date, qe.from_station_telecode, qe.to_station_telecode);
                if (queryTicketResponseInfo != null && queryTicketResponseInfo.status)
                {
                    if (queryTicketResponseInfo.data != null && queryTicketResponseInfo.data.Count > 0)
                    {
                        string[] array = qe.start_time_str.Split("--".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        int num = Convert.ToInt32(array[0].Replace(":", ""));
                        int num2 = Convert.ToInt32(array[1].Replace(":", ""));
                        foreach (QueryLeftNewDTOData current in queryTicketResponseInfo.data)
                        {
                            bool flag = TicketHelper.CheckTrainClass(current, qe);
                            if (flag)
                            {
                                int num3 = Convert.ToInt32(current.queryLeftNewDTO.start_time.Replace(":", ""));
                                if (num3 >= num && num2 >= num3)
                                {
                                    if ((qe.trainNoList != null && qe.trainNoList.Count > 0 && qe.trainNoList.Contains(current.queryLeftNewDTO.station_train_code)) || qe.trainNoList == null || qe.trainNoList.Count == 0)
                                    {
                                        DataRow dataRow = dataTable.NewRow();
                                        dataRow["TrainCode"] = current.queryLeftNewDTO.station_train_code;
                                        dataRow["TrainCodeKey"] = string.Concat(new string[]
                                        {
                                            current.queryLeftNewDTO.train_no,
                                            "#",
                                            current.queryLeftNewDTO.start_station_telecode,
                                            "#",
                                            current.queryLeftNewDTO.to_station_telecode
                                        });
                                        dataRow["StartStation"] = current.queryLeftNewDTO.from_station_name + "(" + current.queryLeftNewDTO.start_time + ")";
                                        dataRow["AimStation"] = current.queryLeftNewDTO.to_station_name + "(" + current.queryLeftNewDTO.arrive_time + ")";
                                        dataRow["TakeTime"] = current.queryLeftNewDTO.lishi;
                                        dataRow["ShangWu"] = current.queryLeftNewDTO.swz_num;
                                        dataRow["TeDeng"] = current.queryLeftNewDTO.tz_num;
                                        dataRow["YiDeng"] = current.queryLeftNewDTO.zy_num;
                                        dataRow["ErDeng"] = current.queryLeftNewDTO.ze_num;
                                        dataRow["GaoJiRuanWo"] = current.queryLeftNewDTO.gr_num;
                                        dataRow["RuanWo"] = current.queryLeftNewDTO.rw_num;
                                        dataRow["YingWo"] = current.queryLeftNewDTO.yw_num;
                                        dataRow["RuanZuo"] = current.queryLeftNewDTO.rz_num;
                                        dataRow["YingZuo"] = current.queryLeftNewDTO.yz_num;
                                        dataRow["WuZuo"] = current.queryLeftNewDTO.wz_num;
                                        dataRow["QiTa"] = current.queryLeftNewDTO.qt_num;
                                        dataRow["OrderKey"] = current.secretStr;
                                        bool flag2 = current.queryLeftNewDTO.to_station_name == current.queryLeftNewDTO.end_station_name;
                                        dataRow["Last"] = flag2;
                                        bool flag3 = current.queryLeftNewDTO.from_station_name == current.queryLeftNewDTO.start_station_name;
                                        dataRow["First"] = flag3;
                                        dataRow["Order"] = null;
                                        dataTable.Rows.Add(dataRow);
                                    }
                                }
                            }
                        }
                        dataTable = TicketHelper.FilterResultBySeat(dataTable, qe);
                    }
                }
                if (dataTable.Rows.Count > 0)
                {
                    break;
                }
            }
            return dataTable;
        }

        public static bool CheckTrainClass(QueryLeftNewDTOData q, QueryEntity qe)
        {
            bool flag = false;
            if (qe.trainClass.Contains("QB"))
            {
                flag = true;
            }
            else
            {
                List<string> list = qe.trainClass.Split("#".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList<string>();
                string trainCode = q.queryLeftNewDTO.station_train_code.Substring(0, 1).ToUpper();
                list.ForEach(delegate(string t)
                {
                    if (t.Contains(trainCode))
                    {
                        flag = true;
                    }
                });
            }
            return flag;
        }

        private static DataTable FilterResultBySeat(DataTable dt, QueryEntity qe)
        {
            DataTable dataTable = dt.Clone();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (!TicketHelper.FilterRowBySeat(dt.Rows[i], qe.seatTypeList))
                {
                    DataRow dataRow = dataTable.NewRow();
                    for (int j = 0; j < dataTable.Columns.Count; j++)
                    {
                        dataRow[j] = dt.Rows[i][j];
                    }
                    dataTable.Rows.Add(dataRow);
                }
            }
            dataTable.AcceptChanges();
            return dataTable;
        }

        private static bool FilterRowBySeat(DataRow row, List<string> list)
        {
            bool result = true;
            foreach (string current in list)
            {
                string text = row[current].ToString().Trim();
                if (text.IndexOf("有") > -1)
                {
                    result = false;
                    break;
                }
            }
            return result;
        }

        public static TicketInfo GetTicketInfo(string secretStr, QueryEntity qe, ref string msg)
        {
            TicketInfo result = null;
            TicketService ticketService = new TicketService(Context.Cookies);
            bool flag = ticketService.SubmitOrderRequest(secretStr, qe, ref msg);
            if (flag)
            {
                result = ticketService.GetTicketInfo();
            }
            return result;
        }

        public static void GetPassengers()
        {
            TicketService ticketService = new TicketService(Context.Cookies);
            GetPassengerDTOsResponseInfo myPassengers = ticketService.GetMyPassengers();
            Context.Passengers = myPassengers.data.normal_passengers;
        }

        public static bool CheckOrderInfo(string token, string vc, List<PassengerInfo> list, ref string msg)
        {
            TicketService ticketService = new TicketService(Context.Cookies);
            return ticketService.CheckOrderInfo(token, vc, list, ref msg);
        }

        public static CountInfo GetQueueCount(TicketInfo ti, string seatType)
        {
            TicketService ticketService = new TicketService(Context.Cookies);
            return ticketService.GetQueueCount(ti, seatType);
        }

        public static bool ConfirmSingleForQueue(TicketInfo ti, string vc, List<PassengerInfo> list, ref string msg)
        {
            TicketService ticketService = new TicketService(Context.Cookies);
            return ticketService.ConfirmSingleForQueue(ti, vc, list, ref msg);
        }

        public static QueryWaitTimeResult QueryOrderWaitTime(string token)
        {
            TicketService ticketService = new TicketService(Context.Cookies);
            return ticketService.QueryOrderWaitTime(token);
        }

        public static List<TrainDetails> GetTrainDetails(string train_no, string start_station, string aim_station, string date)
        {
            TicketService ticketService = new TicketService(Context.Cookies);
            return ticketService.GetTrainDetails(train_no, start_station, aim_station, date);
        }


        //---------------------------------------------------------------------------------------------------------


        public static List<QueryLeftNewDTOData> GetLeftTicket(QueryEntity qe)
        {
            var ticketService = new TicketService(Context.Cookies);
            var trains = new List<QueryLeftNewDTOData>();
            foreach (var t in qe.optionDates)
            {
                qe.train_date = t;
                var queryTicketResponseInfo = ticketService.QueryLeftTicketDTO(qe.train_date, qe.from_station_telecode, qe.to_station_telecode);
                if (queryTicketResponseInfo == null) continue;
                trains = trains.Concat(queryTicketResponseInfo.data).ToList();
            }
            return trains;
        }

        public static List<QueryLeftNewDTOData> GetTrainDatas(string date, string from, string to)
        {
            var ticketService = new TicketService(Context.Cookies);
            var queryTicketResponseInfo = ticketService.QueryLeftTicketDTO(date, from, to);
            if (queryTicketResponseInfo == null || queryTicketResponseInfo.data == null) return null;
            return queryTicketResponseInfo.data;
        }

        public static string GetNoCompleteData()
        {
            var ticketService = new TicketService(Context.Cookies);
            var data = ticketService.QueryMyOrderNoComplete();
            if (data == null || data.data == null || data.data.orderDBList == null || data.data.orderDBList.Count < 1) return string.Empty;
            var sb = new StringBuilder();
            var item = data.data.orderDBList[0];
            var pass = item.array_passser_name_page.Aggregate("", (c, s) => c + s + ",").TrimEnd(',');
            sb.AppendFormat("[{0}]{1}→{2}(订单号：{3})乘客[{4}] {5} 开.", item.train_code_page, item.from_station_name_page[0], item.to_station_name_page[0], item.sequence_no, pass, item.start_train_date_page);
            return sb.ToString();
        }

        //----------------------------------------------------------------------------------------------------------

        public static bool HasTicket(QueryLeftNewDTOData data, Seat seat)
        {
            var flag = false;
            var st = (SeatType)Enum.Parse(typeof(SeatType), seat.Text);
            switch (st)
            {
                case SeatType.商务座:
                    flag = CheckTicket(data.queryLeftNewDTO.swz_num);
                    break;
                case SeatType.特等座:
                    flag = CheckTicket(data.queryLeftNewDTO.tz_num);
                    break;
                case SeatType.一等座:
                    flag = CheckTicket(data.queryLeftNewDTO.zy_num);
                    break;
                case SeatType.二等座:
                    flag = CheckTicket(data.queryLeftNewDTO.ze_num);
                    break;
                case SeatType.高级软卧:
                    flag = CheckTicket(data.queryLeftNewDTO.gr_num);
                    break;
                case SeatType.软卧:
                    flag = CheckTicket(data.queryLeftNewDTO.rw_num);
                    break;
                case SeatType.硬卧:
                    flag = CheckTicket(data.queryLeftNewDTO.yw_num);
                    break;
                case SeatType.软座:
                    flag = CheckTicket(data.queryLeftNewDTO.rz_num);
                    break;
                case SeatType.硬座:
                    flag = CheckTicket(data.queryLeftNewDTO.yz_num);
                    break;
                case SeatType.无座:
                    flag = CheckTicket(data.queryLeftNewDTO.wz_num);
                    break;
            }
            return flag;
        }

        public static bool CheckTicket(string num)
        {
            return num != "无" && num != "--" && num != "*";
        }

    }
}
